<?php

class ImagesController extends AppController {

    var $name = 'Images';

    function index() {
        $this->Image->recursive = 0;
        $this->set('images', $this->paginate());
    }

    function admin_index() {
        $this->Image->recursive = 0;
        $this->set('images', $this->paginate());
    }

    function indexc($categoria=null) {

        if ($categoria == null) {

            $this->redirect(array('controller' => 'categories', 'action' => 'index'));
        }

        $this->paginate = array(
            'Image' => array(
                'conditions' => array('Image.categorie_id' => $categoria),
            ),
        );

        $this->set('images', $this->paginate());
    }

    function admin_indexc($categoria=null) {

        if ($categoria == null) {

            $this->redirect(array('controller' => 'categories', 'action' => 'index'));
        }

        $this->paginate = array(
            'Image' => array(
                'conditions' => array('Image.categorie_id' => $categoria),
            ),
        );

        $this->set('images', $this->paginate());
    }

    function admin_view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid image', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('image', $this->Image->read(null, $id));
    }

    function view($id = null) {
        if (!$id) {
            $this->Session->setFlash(__('Invalid image', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->set('image', $this->Image->read(null, $id));
    }

    function admin_add() {
        if (!empty($this->data)) {
            $this->Image->create();

            // how to ensure unique file names?
            // TODO: Code to warn user about duplicate files
            
            $newName = $this->Image->saveImage($this->params['data']['Image']['location'], $this->params['data']['Image']['categorie_id'], 100, "ht", 80);
            
            if (isset($newName)) {
                $this->params['data']['Image']['location'] = $newName;
            } else {
                $this->params['data']['Image']['location'] = null;
                die("La imagen no pudo ser salvada.");
            }

            if ($this->Image->save($this->data)) {
                $this->Session->setFlash(__('¡Imagen guardada con éxito!', true));
                $this->redirect(array('action' => 'indexc'));
            } else {
                $this->Session->setFlash(__('¡La imagen no pudo ser guardada, intentelo de nuevo!', true));
            }
        }
        $categories = $this->Image->Categorie->find('list');
        $tags = $this->Image->Tag->find('list');
        $this->set(compact('categories', 'tags'));
    }

    function admin_edit($id = null) {
        $imageData = $this->Image->read();
        if (!$id && empty($this->data)) {
            $this->Session->setFlash(__('¡Imagen inválida!', true));
            $this->redirect(array('action' => 'index'));
        }

        if (!empty($this->data)) {
            // Is there a new picture?
            if ($this->params['data']['Image']['location']['error'] == 0) {
                // Delete the picture
                $this->Image->delImage($imageData['Image']['location']);
                $newName = $this->Image->saveImage($this->params['data']['Image']['location'], 100, "ht", 80);
                if (isset($newName)) {
                    $this->params['data']['Image']['location'] = $newName;
                } else {
                    $this->params['data']['Image']['location'] = null;
                    // TODO: Write code to graciously exit if Photo::saveImage fails for now just die()
                    die("La imagen no pudo ser salvada.");
                }
            } else {
                // no new picture so keep the old link-location
                $this->params['data']['Image']['location'] = $imageData['Image']['location'];
            }
            ##
            if ($this->Image->save($this->data)) {
                $this->Session->setFlash(__('¡Imagen modificada con éxito!', true));
                $this->redirect(array('action' => 'indexc'));
            } else {
                $this->Session->setFlash(__('The image could not be saved. Please, try again.', true));
            }
            ##
        }
        if (empty($this->data)) {
            $this->data = $this->Image->read(null, $id);
        }
        $categories = $this->Image->Categorie->find('list');
        $tags = $this->Image->Tag->find('list');
        $this->set(compact('categories', 'tags'));
    }

    function admin_delete($id = null) {
        $imageData = $this->Image->read();
        if (!$id) {
            $this->Session->setFlash(__('Invalid id for image', true));
            $this->redirect(array('action' => 'index'));
        }
        if ($this->Image->delete($id)) {
            $this->Image->delImage($imageData['Image']['location'], $imageData['Image']['categorie_id']);
            $this->Session->setFlash(__('Imagen Eliminada', true));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Image was not deleted', true));
        $this->redirect(array('action' => 'index'));
    }

}

?>